package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.EqpEntoty;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;

public class RecipeUpDataActivity extends BaseDialogActivity {


    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.flRecipeName)
    FrameLayout flRecipeName;
    @BindView(R.id.tvRecipeName)
    TextView tvRecipeName;
    @BindView(R.id.ivRecipeName)
    ImageView ivRecipeName;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;

    private FreeUI_GeneralPop mNamePop;
    private ArrayList<String> recipeList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_updata;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("PE程序上传");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View s) {
                UploadRecipe();
            }
        });
    }


    @Override
    protected void initData() {

        etEqpId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    requestEqpInfo(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        flRecipeName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNamePop != null) {
                    if (mNamePop.isShowing()) {
                        mNamePop.dismiss();
                    }
                } else {
                    mNamePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flRecipeName.getWidth(), recipeList, ivRecipeName, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {
                            tvRecipeName.setText(s);
                        }
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivRecipeName);
                mNamePop.showAsDropDown(flRecipeName, 0, 0);
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    private void requestEqpInfo(final String eqpID) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("getEqpInfo","GetEqpInfo",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<EqpEntoty>>(
                new TypeToken<ResponseBean<EqpEntoty>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<EqpEntoty>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    tvEqpType.setText(response.body().getBody().getEqpType());
                    getEqpRecipeList(eqpID);
                }
            }

            @Override
            public void onError(Response<ResponseBean<EqpEntoty>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEqpInfo");
            }
        });
    }

    private void getEqpRecipeList(String eqpID) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", eqpID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("getEqpRecipeList","GetEqpRecipeList",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<EqpEntoty>>(
                new TypeToken<ResponseBean<EqpEntoty>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<EqpEntoty>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    recipeList.addAll(response.body().getBody().getList());
                }
            }

            @Override
            public void onError(Response<ResponseBean<EqpEntoty>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEqpRecipeList");
            }
        });
    }

    private void UploadRecipe() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EqpID", etEqpId.getText().toString());
            jsonObject.put("RecipeName", tvRecipeName.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("uploadRecipe","UploadRecipe",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<Object>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("Recipe上传成功",true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    },2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "UploadRecipe");
            }
        });
    }

}
